// components/cus-navigation-bar/index.js
import {getNavSize} from "../../utils/system"
import {Score} from "../../model/score"
const COUNT_DOWN_TIME = 30
const COUNT_DOWN_END_TIME = 0
Component({

    properties: {
    	countStatus: Boolean,
	    time: Number,
		checkpoints: Object,
		type: String
    },
	interval: null,
	data: {
		statusBarHeight: Number,
		navigationHeight: Number,
		countDownTime: COUNT_DOWN_TIME,
		isStop: false
	},

	pageLifetimes: {
		show: async function () {
			let group = await getNavSize()
			this.setData({
				statusBarHeight: group.statusBarHeight,
				navigationHeight: group.navigationHeight
			})
		}
	},

	observers: {
		'countStatus': function (countStatus) {
			if (countStatus === true) {
				if (this.data.isStop === true) {
					this.setData({
						countDownTime: COUNT_DOWN_TIME,
						isStop: false
					})
				}
				this.interval = setInterval(() => {
					const countDownTime = this.data.countDownTime
					if (countDownTime === COUNT_DOWN_END_TIME) {
						// this.triggerEvent("calccounttime", {
						// 	countDownTime: COUNT_DOWN_TIME - this.data.countDownTime
						// })
						this.emitCountDownTime()
						return
					}
					this.setData({
						countDownTime: (countDownTime - 1) < 10 ? '0'+(countDownTime - 1): (countDownTime - 1)
					})
					this.emitCountDownTime()

				}, 1000)
			} else {
				this.setData({
					isStop: true
				})
				clearInterval(this.interval)
			}
		}
	},

    methods: {
    	emitCountDownTime() {
		    this.triggerEvent("calccounttime", {
			    countDownTime: COUNT_DOWN_TIME - this.data.countDownTime
		    })
	    },
	    onBack: function() {
			this.triggerEvent("back");
			clearInterval(this.interval)
			wx.navigateBack({
				delta: 1,
			})
			// if (this.properties.type == 'pk') {
			// 	this.triggerEvent("back");
			// 	clearInterval(this.interval)
			// 	wx.navigateBack({
			// 		delta: 1,
			// 	})
			// }else {
			// 	let that = this
			// 	wx.showModal({
			// 		title: '提示',
			// 		content: '中途退出将消耗一次答题次数,并且分数为0, 确定退出?',
			// 		async success(res) {
			// 			if (res.confirm) {
			// 				console.log('用户点击确定')
			// 				const score = await Score.uploadPoint({
			// 					point: 0,
			// 					duration: 0,
			// 					checkpoint_id: that.properties.checkpoints.id
			// 				})
			// 				that.triggerEvent("back");
			// 				clearInterval(that.interval)
			// 				wx.navigateBack({
			// 					delta: 1,
			// 				})
			// 			} else if (res.cancel) {
			// 				console.log('用户点击取消')
			// 			}
			// 		}
			// 	})
			// }
	    }
    }
})
